<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>激活系统</title>
    <link type="text/css" rel="stylesheet" th:href="${getSkinPath}+'/css.css'"/>
    <script th:src="@{/inc/common.js}"></script>
    <script th:src="@{/js/jquery-1.9.1.min.js}" type="text/javascript"></script>
    <script th:src="@{/js/jquery-migrate-1.2.1.min.js}"></script>
    <script th:src="@{/js/jquery-alerts/jquery.alerts.js}" type="text/javascript"></script>
    <script th:src="@{/js/jquery-alerts/cws.alerts.js}" type="text/javascript"></script>
    <script th:src="@{/js/layui/layui.js}" charset="utf-8"></script>
    <link th:href="@{/js/jquery-alerts/jquery.alerts.css}" rel="stylesheet" type="text/css" media="screen"/>
    <script type="text/javascript" th:src="@{/js/activebar2.js}"></script>
    <link rel="stylesheet" th:href="@{/js/layui/css/layui.css}" media="all">
    <link th:href="@{/js/jquery-showLoading/showLoading.css}" rel="stylesheet" media="screen"/>
    <script type="text/javascript" th:src="@{/js/jquery-showLoading/jquery.showLoading.js}"></script>
</head>
<body>
<table align="center" class="tabStyle_1 percent60" style="margin-top: 20px">
    <tbody>
    <tr>
        <td align="left" class="tabStyle_1_title">激活系统</td>
    </tr>
    <tr>
        <td colspan="2" style="line-height: 2">
            <p>
                授权单位：<span th:text="${lic.company}"></span><br>
                用户数：<span th:text="${userCount}"></span><br>
                类型：<span th:text="${type}"></span><br>
                到期时间：<span th:text="${dateFormat}"></span><br>
                域名：<span th:text="${domain}"></span><br>
            </p>
        </td>
    </tr>
    <tr>
        <td height="30" colspan="2" align="center">
            <input id="btnActivation" type="button" class="btn" value="在线激活">
            &nbsp;&nbsp;&nbsp;&nbsp;
            <input id="btnOfflineActivate" type="button" class="btn" value="离线激活"
                   th:onclick="window.location.href = 'sysActivateOffline'">
            &nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" class="btn" value="返回" onclick="window.history.back();">
        </td>
    </tr>
    </tbody>
</table>
<form id="myForm"></form>
<script th:inline="javascript">
    $('#btnActivation').click(function () {
        // 下载license文件
        var params = null;
        var data = {};
        if (data) {
            params = JSON.stringify(data);
        }

        var xhr = new XMLHttpRequest();
        xhr.open('POST', '../public/lic/download', true);
        xhr.responseType = 'blob';
        xhr.onload = function () {
            if (this.status === 200) {
                var blob = this.response;
                var filename = "";
                var disposition = xhr.getResponseHeader('Content-Disposition');
                if (disposition && disposition.indexOf('attachment') !== -1) {
                    var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
                    var matches = filenameRegex.exec(disposition);
                    if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
                }

                var formData = new FormData($('#myForm')[0]);
                var server = window.location.host.split(":")[0];
                console.log('server', server);
                var activateRequestCode = [[${activateRequestCode}]];
                formData.append("activateRequestCode", activateRequestCode);
                formData.append("filename0", blob, "license.dat");
                $.ajax({
                    async: false,
                    type: "post",
                    url: [[${url}]],
                    data: formData,
                    cache: false,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    beforeSend: function (XMLHttpRequest) {
                        $('body').showLoading();
                    },
                    success: function (data, status) {
                        if (data.ret == 1) {
                            // 上传激活码
                            $.ajax({
                                type: "post",
                                url: "../public/lic/setActivationCodeV2.do?ac=" + data.activationCode,
                                data: {},
                                dataType: "html",
                                beforeSend: function (XMLHttpRequest) {
                                    $("body").showLoading();
                                },
                                success: function (data, status) {
                                    data = $.parseJSON(data);
                                    if (data.ret == 1) {
                                        layer.msg(data.msg);
                                    }
                                },
                                complete: function (XMLHttpRequest, status) {
                                    $("body").hideLoading();
                                },
                                error: function (XMLHttpRequest, textStatus) {
                                    // 请求出错处理
                                    alert(XMLHttpRequest.responseText);
                                }
                            });
                        } else {
                            layer.msg(data.msg);
                        }
                    },
                    complete: function (XMLHttpRequest, status) {
                        $('body').hideLoading();
                    },
                    error: function () {
                        //请求出错处理
                        alert(XMLHttpRequest.responseText);
                    }
                });
            }
        };
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xhr.send(params);
    })

    $(function () {
        $('#btnCopy').click(function () {
            o('offlineCode').select();
            if (document.execCommand('copy')) {
                document.execCommand('copy');
            }
            layer.msg("复制成功！", '提示');
        })
    })
</script>
</body>
</html>
